package org.lql.algo.codecrush.hot100.binarySearch;

/**
 * @author: liangqinglong
 * @date: 2025-09-05 13:49
 * @description: 35. 搜索插入位置 <a href="https://leetcode.cn/problems/search-insert-position/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class SearchInsert {

	public int searchInsert(int[] nums, int target) {
		int n = nums.length;
		int left = 0;
		int right = n - 1;
		while (left <= right) {
			int mid = (left + right) / 2;
			if (nums[mid] == target) {
				return mid;
			} else if (nums[mid] < target) {
				left = mid + 1;
			} else {
				right = mid - 1;
			}
		}
		return left;
	}

	public static void main(String[] args) {
		int[] nums = {1, 3, 5, 6};
		int target = 2;
		SearchInsert searchInsert = new SearchInsert();
		int i = searchInsert.searchInsert(nums, target);
		System.out.println(i);
	}
}
